package cn.com.scitc.pano.controller;

import cn.com.scitc.pano.file.NameFilter;
import cn.com.scitc.pano.tools.DeleteFile;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.File;


@Controller
public class WorkController {
    private Log log = LogFactory.getLog(getClass());
    public String[] workna;
    public String username;

    @GetMapping("/userwork")
    public String UserWork(Model model){
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = userDetails.getUsername();
        this.username = name;
        String[] imagename = new String[30];
        File file = new File("D:/code/pano/src/main/resources/static/images/rotPhoto/" + name + "/" );
        NameFilter filter = new NameFilter("jpeg");
        File[] lf = file.listFiles(filter);
        for ( int i =0; i < lf.length; i++) {
            imagename[i] = lf[i].getName();
        }
        model.addAttribute("name", name);
        GoodsController.goodsname(model, name, imagename, file);
        return "userwork";
    }

    @RequestMapping("/userwork")
    @ResponseBody
    public String userwork(String[] ids) {
        this.workna = ids;
        return "userwork";
    }

    @RequestMapping("/del")
    public String del(){
        for (int i = 0;i < workna.length; i++){
            File file = new File("D:/code/pano/src/main/resources/static/" + workna[i]);
            file.delete();
        }
        for (int n = 0;n < workna.length; n++){
            File fileone = new File("D:/code/pano/src/main/resources/static/" + workna[n].substring(0,workna[n].length() - 5));
            DeleteFile deleteFile = new DeleteFile();
            deleteFile.deleteFile(fileone);
        }
        return "del";
    }
}

